#include "scores.h"

Scores* Scores::ptr = NULL;

Scores::Scores (void)
{
	ptr = this;
}

BOOL CALLBACK Scores::DlgProc(HWND hWnd, UINT mes, WPARAM wParam, LPARAM lParam)
{
	switch(mes)
	{
		HANDLE_MSG(hWnd,WM_INITDIALOG,ptr->Cls_OnInitDialog);
		HANDLE_MSG(hWnd,WM_CLOSE,ptr->Cls_OnClose);
		HANDLE_MSG(hWnd,WM_COMMAND,ptr->Cls_OnCommand);
	}
	return FALSE;
}

BOOL Scores::Cls_OnInitDialog (HWND hWnd, HWND hWndFocus, LPARAM lParam)
{
	//hDlg = hWnd;
	//HWND hBut = GetDlgItem (hWnd, IDC_STATIC);
	//SetWindowText (hBut, TEXT("HHH"));
	int err = GetLastError();
	for (int i = 0; i < 3; i ++)
	{
		//hName[i] = GetDlgItem(hWnd, IDC_NAME1+i);
		//hSteps[i] = GetDlgItem(hWnd, IDC_STEPS1+i);
		//hTime[i] = GetDlgItem(hWnd, IDC_TIME1+i);
	}

	HANDLE hFile = CreateFile(TEXT("score.txt"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	TCHAR ReadBuffer[200] = {0};
	DWORD  dwBytesRead = 0;

	int isWrite = ReadFile(hFile, ReadBuffer, 199, &dwBytesRead, NULL);

	/*TCHAR* name;
	name = (TCHAR*)strtok((char*)ReadBuffer, (char*)'&');
	int i = 0;
	do
	{
		SetWindowText (hName[i], name);
		name = (TCHAR*)strtok((char*)'\0', (char*)'&');
		i ++;
	}
	while (name!= NULL);*/
	//hDlg = hWnd;
	
	return TRUE;
}

void Scores::Cls_OnClose (HWND hWnd)
{
	EndDialog(hWnd, 0);
}

void Scores::Cls_OnCommand (HWND hWnd, int id, HWND hWndCtl, UINT codeNotify)
{
	if (id = IDC_UP)
	{
		/*for (int i = 0; i < 3; i ++)
		{
			hName[i] = GetDlgItem(hDlg, IDC_NAME1+i);
			hSteps[i] = GetDlgItem(hDlg, IDC_STEPS1+i);
			hTime[i] = GetDlgItem(hDlg, IDC_TIME1+i);
		}*/

		HANDLE hFile = CreateFile(TEXT("score.txt"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
		TCHAR ReadBuffer[200] = {0};
		DWORD  dwBytesRead = 0;

		int isWrite = ReadFile(hFile, ReadBuffer, 199, &dwBytesRead, NULL);

		TCHAR* name;
		name = (TCHAR*)strtok((char*)ReadBuffer, (char*)'&');
		SetWindowText (GetDlgItem(hWnd, IDC_UP), name);
		int er = GetLastError ();
		int i = 0;
		do
		{
			SetWindowText (GetDlgItem(hWnd, IDC_UP), name);
			name = (TCHAR*)strtok((char*)'\0', (char*)'&');
			i ++;
		}
		while (name!= NULL);
	}
}
